package com.kehutong.repair.admin;

import java.util.List;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.repair.enums.OperationType;
import com.kehutong.repair.enums.TimeUnit;
import com.kehutong.repair.service.admin.AdminRepairsTypeService;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.util.EnumUtil;

import com.kehutong.repair.entity.RepairsType;

@MvcAction
@ReqMapping("/admin/repairs/type")
public class AdminRepairsTypeController {

    static final Logger logger = LoggerFactory.getLogger(AdminRepairsTypeController.class);

	@Inject
	private JdbcSession jdbcSession;

	@Inject
    private AdminRepairsTypeService adminRepairsTypeService;

    @Inject
    private DoveClient doveClient;
	
    @HasPermission("admin:repairsType:view")
    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) throws Exception {
        final Page<RepairsType> page = adminRepairsTypeService.list(jsonObject);

        List<RepairsType> list = page.getList();
        list.forEach(repairsType -> {
            try {
                setUnSaveField(token.getCompanyNo(), repairsType);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        return Result.success(page);
    }

    @HasPermission("admin:repairsType:view")
    @ReqMapping("/allList")
    public Object allList(JSONObject jsonObject) {
        return adminRepairsTypeService.allList(jsonObject);
    }

    @HasPermission("admin:repairsType:view")
    @ReqMapping("/get")
    public Object get(Token token, String id) throws Exception {
        RepairsType item = adminRepairsTypeService.get(id);
        if (item == null) {
            return Result.error(0, "对象不存在");
        }

        setUnSaveField(token.getCompanyNo(), item);
        return Result.success(item);
    }

    @Bean(newInstance = true, copy = true)
    @HasPermission("admin:repairsType:edit")
    @ReqMapping("/save")
    public Object save(RepairsType repairsType, JSONObject jsonObject) {
        repairsType.setUnit(EnumUtil.getEnum(TimeUnit.class, jsonObject.getString("unit")));
        repairsType.setType(EnumUtil.getEnum(OperationType.class, jsonObject.getString("type")));
        repairsType.setDeleted(false);

        return adminRepairsTypeService.save(repairsType);
    }

    @Bean(copy = true)
    @HasPermission("admin:repairsType:edit")
    @ReqMapping("/update")
    public Object update(RepairsType repairsType, JSONObject jsonObject) {
        repairsType.setUnit(EnumUtil.getEnum(TimeUnit.class, jsonObject.getString("unit")));
        return adminRepairsTypeService.update(repairsType);
    }

    @HasPermission("admin:repairsType:edit")
    @ReqMapping("/delete")
    public Object delete(String id) {
        return adminRepairsTypeService.delete(id);
    }

    private void setUnSaveField(String companyNo, RepairsType repairsType) throws Exception {
        doveClient.post("/flow/service/role/get", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.addParam("id", repairsType.getExeRoleId());
        }, data ->{
            repairsType.setExeRoleName(data.getString("name"));
        });

        doveClient.post("/flow/service/role/get", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.addParam("id",  repairsType.getAttnRoleId());
        },  data ->{
            repairsType.setAttnRoleName(data.getString("name"));
        });
    }
}
